package com.lyshop.jinding.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.lyshop.jinding.R;
import com.lyshop.jinding.bean.PayTypeBean;
import com.lyshop.jinding.bean.UserInfoBean;
import com.lyshop.jinding.utils.DataUtils;

import java.util.List;

/**
 * Created by Mr on 2018/4/8.
 */

public class RechargeQuotaGridAdapter extends CommonAdapter<PayTypeBean.RechargeAmounts> {
    private final UserInfoBean mUserInfo;
    private int mCheckedPosition = 0;
    private String mQuotaDesc = "";

    public RechargeQuotaGridAdapter(Context context, OnCheckListener listener) {
        super(context);
        mUserInfo = DataUtils.getUserInfo(context);
        this.mListener = listener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_recharge_quoat, parent, false);
            holder.findViews(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setData(mDatas.get(position), position);
        return convertView;
    }

    class ViewHolder implements View.OnClickListener {
        private RadioButton bt_recharge_quota;
        private TextView tv_remark;
        private int mPosition = 0;

        private void findViews(View rootView) {
            bt_recharge_quota = rootView.findViewById(R.id.bt_recharge_quota);
            tv_remark = rootView.findViewById(R.id.tv_remark);
            bt_recharge_quota.setOnClickListener(this);
        }

        private void setData(PayTypeBean.RechargeAmounts quota, int position) {
            mPosition = position;
            bt_recharge_quota.setText(quota.amount + "元");
            bt_recharge_quota.setChecked(position == mCheckedPosition);

            if (!TextUtils.isEmpty(quota.tips) && quota.activity == 1 && mUserInfo != null && mUserInfo.money_sum <= 0) {
                tv_remark.setVisibility(View.VISIBLE);
                tv_remark.setText(quota.tips);
            } else {
                tv_remark.setVisibility(View.GONE);
            }
        }

        @Override
        public void onClick(View view) {
            mCheckedPosition = mPosition;
            mListener.onCheckPosition(mCheckedPosition);
            notifyDataSetChanged();
        }
    }

    @Override
    public void setDataSource(List<PayTypeBean.RechargeAmounts> list) {
        mDatas.clear();
        mDatas.addAll(list);
        notifyDataSetChanged();
    }

    public void setCheckedPosition(int checkedPosition) {
        mCheckedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public int getCheckedPosition() {
        return mCheckedPosition;
    }

    private OnCheckListener mListener;

    public interface OnCheckListener {
        void onCheckPosition(int position);
    }


}
